﻿using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using WeChatIdentity.Enums;

namespace WeChatIdentity.Models
{
    public class ApplicationUser : IdentityUser
    {
        [Required]
        public string OpenId { get; set; }

        public string SubjectId { set; get; }
        /// <summary>
        /// 昵称
        /// </summary>
        public string NickName { set; get; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 头像
        /// </summary>
        public string AvatarUrl { get; set; }
        /// <summary>
        /// 性别
        /// </summary>
        public string Gender { get; set; }
        /// <summary>
        /// 省
        /// </summary>
        public string Province { get; set; }
        /// <summary>
        /// 市
        /// </summary>
        public string City { get; set; }
        /// <summary>
        /// 国家
        /// </summary>
        public string Country { get; set; }

        //private readonly List<UserClaim> _userClaims;
        //public IReadOnlyCollection<UserClaim> UserClaims => _userClaims;

        protected ApplicationUser()
        {
            //_userClaims = new List<UserClaim>();
            this.CreateTime = DateTime.Now;
        }
        public ApplicationUser(string userName,string openId,string nickName,
            string avatarUrl,string gender,string province,string city,string country)
        {
            this.UserName = userName;
            this.OpenId = openId;
            this.NickName = nickName;
            this.AvatarUrl = avatarUrl;
            this.Gender = gender;
            this.Province = province;
            this.City = city;
            this.Country = country;
            this.TwoFactorEnabled = false;
            this.PhoneNumberConfirmed = false;
            this.EmailConfirmed = false;
            this.LockoutEnabled = false;
            this.AccessFailedCount = 0;
            this.Id = Guid.NewGuid().ToString("N");
            this.SubjectId = this.Id;
            this.CreateTime = DateTime.Now;
            //_userClaims = new List<UserClaim>();
        }

        public ApplicationUser(string userName, string openId, string subjectId, string nickName)
        {
            this.UserName = userName;
            this.OpenId = openId;
            this.NickName = nickName;
            this.TwoFactorEnabled = false;
            this.PhoneNumberConfirmed = false;
            this.EmailConfirmed = false;
            this.LockoutEnabled = false;
            this.AccessFailedCount = 0;
            this.Id = Guid.NewGuid().ToString("N");
            this.SubjectId = this.Id;
            this.CreateTime = DateTime.Now;
            //_userClaims = new List<UserClaim>();
        }
        /// <summary>
        /// 绑定电话号
        /// </summary>
        /// <param name="phonenum"></param>
        public void SetPhoneNumber(string phonenum)
        {
            this.PhoneNumber = phonenum;
            this.PhoneNumberConfirmed = true;
        }
        /// <summary>
        /// 设置身份
        /// </summary>
        /// <param name="claimType">身份类型</param>
        /// <param name="claimValue">身份值</param>
        //public void SetClaims(string claimType,string claimValue)
        //{
        //    if (_userClaims.Any())
        //    {
        //       var userClaim = _userClaims.FirstOrDefault(t => t.ClaimType == claimType);
        //        if(userClaim != null)
        //        {
        //            userClaim.ClaimValue = claimValue;
        //        }
        //    }
        //    else
        //    {
        //        _userClaims.Add(new UserClaim(claimType, claimValue));
        //    }
        //}
    }
}
